package com.example.springbatch;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import com.example.domain.Book;

public class BookWriteDao implements ItemWriter<Book> {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public void write(final List<? extends Book> items) throws Exception {
		jdbcTemplate.batchUpdate("insert into bookback values(?,?)", new BatchPreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				Book book=items.get(i);
				ps.setString(1, book.getId());
				ps.setString(2, book.getName());
			}
			
			@Override
			public int getBatchSize() {
				// TODO Auto-generated method stub
				return items.size();
			}
		});
		
	}

}
